package com.hq.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hq.bean.DD;
import com.hq.bean.User;
import com.hq.bean.Wuliao;
import com.hq.service.DDSerivce;
import com.hq.util.SnowflakeIdUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class DDController {
    String snowid="";
    @Resource
    DDSerivce ddSerivce;

    @RequestMapping("/login.do")
    public String login(User user, HttpSession session){
        int i = ddSerivce.login(user);
        if(i>0){
            session.setAttribute("user",user);
            return "redirect:/list.do";
        }else{
            return "redirect:index.jsp";
        }
    }

    @RequestMapping("/list.do")
    public String list(@RequestParam(defaultValue = "1")Integer pageNum, Model model,
                       String createdatex,
                       String createdated,
                       String pic,
                       String zzname,
                       String shenhe,
                       String typezx,
                       String typezd,
                       String typecx,
                       String typecd,
                       String ordername){
        Page<Object> page = PageHelper.startPage(pageNum, 3);
        List list=ddSerivce.list(createdatex,
                createdated,
                pic,
                zzname,
                shenhe,
                typezx,
                typezd,
                typecx,
                typecd,
                ordername);
        model.addAttribute("list",list);
        model.addAttribute("page",page);
        model.addAttribute("createdatex",createdatex);
        model.addAttribute("createdated",createdated);
        model.addAttribute("pic",pic);
        model.addAttribute("zzname",zzname);
        model.addAttribute("shenhe",shenhe);
        model.addAttribute("typezx",typezx);
        model.addAttribute("typezd",typezd);
        model.addAttribute("typecx",typecx);
        model.addAttribute("typecd",typecd);
        model.addAttribute("ordername",ordername);
        return "list";
    }

    @RequestMapping("/toadd.do")
    public String toadd(String snowid){
        return "add";
    }
    @RequestMapping("/getsnowid.do")
    @ResponseBody
    public String getsnowid(){
        if("".equals(snowid)||ddSerivce.listsnow(snowid)>0){
            long l = new SnowflakeIdUtils(1, 1).nextId();
            snowid=l+"";
        }
        return snowid;
    }

    @RequestMapping("/addwl.do")
    public String addwl(Wuliao wuliao){
        int i = ddSerivce.listsnow(wuliao.getWuliao());
        if(i==0){
            ddSerivce.addsnow(wuliao.getWuliao());
        }
        int addwl = ddSerivce.addwl(wuliao);
        if(addwl>0){
            return "forward:/toadd.do?snowid="+wuliao.getWuliao();
        }
        return null;
    }
    @RequestMapping("/findallwl.do")
    @ResponseBody
    public Object findallwl(String wuliao){
        return ddSerivce.findallwl(wuliao);
    }
    @RequestMapping("/delwl.do")
    public String delwl(String wuliao,String wuliaoname){
        int i = ddSerivce.delwl(wuliao, wuliaoname);
        if(i>0){
            return "forward:/toadd.do?snowid="+wuliao;
        }
        return null;
    }
    @RequestMapping("/addan.do")
    public String addan(DD dd){
        int i = ddSerivce.addan(dd);
        if(i>0){
            return "redirect:/list.do";
        }
        return null;
    }
}
